import 'package:core_ui/base/base_logic.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class BaseRefreshLogic<T> extends BaseLogic {
  late RefreshController _refreshController;

  var enableAutoRefresh = true.obs;
  var enableRefresh = true.obs;
  var enableLoadMore = true.obs;

  late RxList<T> data;

  BaseRefreshLogic() : super() {
    data = RxList<T>();
  }

  @override
  void onInit() {
    super.onInit();
    bool auto = true;
    if (enableRefresh.isTrue) {
      auto = enableAutoRefresh.value;
    } else {
      auto = false;
    }
    _refreshController = RefreshController(initialRefresh: auto);
  }

  RefreshController get refreshController =>
      _refreshController; //===Desc:=====================================================================================

  Future<void> onRefreshOrLoadMore(bool isRefresh) async {}

  void onTwoLevel(bool isOpen) {}

  //===Desc:=====================================================================================

  void finishRefresh({bool success = true, bool resetFooterState = false}) {
    if (success) {
      refreshController.refreshCompleted(resetFooterState: resetFooterState);
    } else {
      refreshController.refreshFailed();
    }
  }

  void finishLoadMore({bool success = true, bool noMore = false}) {
    if (success) {
      if (noMore) {
        _refreshController.loadNoData();
      } else {
        _refreshController.loadComplete();
      }
    } else {
      _refreshController.loadNoData();
    }
  }
}
